import {
    Component,
    ElementRef,
    EventEmitter,
    Injector,
    Output,
    ViewChild,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
    PlacesServiceProxy,
    UserPlaceListDto,
    GetUserParkingInfoForEditOutput,
    CreateCarInfoDto,
} from "@shared/service-proxies/service-proxies";
import { id } from "@swimlane/ngx-charts/release/utils";
import { ModalDirective } from "ngx-bootstrap";

import { finalize } from "rxjs/operators";
import {AppConsts} from "../../../shared/AppConsts"

@Component({
    selector: "createOrEditAddlicenseModal",
    templateUrl: "./create-or-edit-add-license-modal.component.html",
})
export class CreateOrEditAddlicenseModalComponent extends AppComponentBase {
    @ViewChild("createOrEditModal") modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    getUserParkingInfoForEditOutput: GetUserParkingInfoForEditOutput =
        new GetUserParkingInfoForEditOutput();

    createCarInfo: CreateCarInfoDto = new CreateCarInfoDto();
    UserPlaceList: UserPlaceListDto = new UserPlaceListDto();
    active = false;
    saving = false;
    parkingCoce: string;
    parkingName: string;
    parkingid: number;
    neighbourhood_Name: string;
    neighbourhood_id: number;
    userid: number;
    placeId: number;
    emptyText: "";

    LicenseColors = AppConsts.LicenseColors;
    constructor(
        injector: Injector,
        private _placesServiceProxy: PlacesServiceProxy
    ) {
        super(injector);
    }
    ngOnInit() {
        this.createCarInfo.licenseColor = this.LicenseColors[0].key;
    }

    show(UserPlaceList: UserPlaceListDto, id: number): void {
        const self = this;
        self.active = true;

        self.neighbourhood_Name = UserPlaceList.neighbourhood_name;
        self.neighbourhood_id = UserPlaceList.neighbourhood_id;
        self.parkingCoce = UserPlaceList.parkingCode;
        self.parkingName = UserPlaceList.parkingName;
        self.parkingid = UserPlaceList.parkingId;
        self.placeId = UserPlaceList.placeId;
        self.userid = id;
        self.modal.show();
    }

    onShown(): void {}

    save(): void {
        if(this.createCarInfo.carNum == undefined || this.createCarInfo.carNum == null || this.createCarInfo.carNum == ""){
            this.notify.error("请输入车辆牌照信息");
            return;
        }
        const self = this;

        this.UserPlaceList.neighbourhood_name = this.neighbourhood_Name;
        this.UserPlaceList.neighbourhood_id = this.neighbourhood_id;
        this.UserPlaceList.parkingId = this.parkingid;
        this.UserPlaceList.parkingCode = this.parkingCoce;
        this.UserPlaceList.parkingName = this.parkingName;
        (this.UserPlaceList.placeId =
            this.getUserParkingInfoForEditOutput.placesId =
                this.placeId),
            (this.createCarInfo.neighbourhood_id = this.neighbourhood_id),
            (this.createCarInfo.neighbourhood_name = this.neighbourhood_Name),
            (this.createCarInfo.parkingCode = this.parkingCoce),
            (this.createCarInfo.parkingId = this.parkingid),
            (this.createCarInfo.parkingName = this.parkingName),
            (this.createCarInfo.carType = 2),
            (this.getUserParkingInfoForEditOutput.userId = this.userid),
            (this.getUserParkingInfoForEditOutput.carInfo = this.createCarInfo);

        console.log(this.getUserParkingInfoForEditOutput);
        console.log(this.UserPlaceList);
        this._placesServiceProxy
            .userPakingForNewLicense(this.getUserParkingInfoForEditOutput)
            .pipe(finalize(() => (this.saving = false)))
            .subscribe(() => {
                this.notify.info(this.l("SavedSuccessfully"));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
